//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by 曹曹 on 16/7/26.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

#define kReuseIdentifier @"reuseIdentifier"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray<NSString *> *nameArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
@property (nonatomic, strong) NSMutableArray<NSString *> *selectDataArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectIndexArray = [NSMutableArray array];
    self.selectDataArray = [NSMutableArray array];
    NSArray *array = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:array];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReuseIdentifier];
    [self.view addSubview:_tableView];
    // tableView编辑
    // 1.设置右barButtonItem 为 editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
#warning action property
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
}
- (void)editAction {
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    if (self.editing) {
        NSLog(@"编辑");
    } else {
        // 要删除选中的数组
        // 删除数据源
        [_nameArray removeObjectsInArray:_selectDataArray];
        // 删除cell
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationLeft];
        // 完成操作后清空暂存数组
        [_selectDataArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
    }
}
#pragma mark - tableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在多选状态下, 数组中添加对应选中的下标
        [_selectIndexArray addObject:indexPath];
        [_selectDataArray addObject:_nameArray[indexPath.row]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在多选状态下, 数组中删除对应选中的下标
        [_selectIndexArray removeObject:indexPath];
        [_selectDataArray removeObject:_nameArray[indexPath.row]];
    }
}
// 4.返回cell的编辑模式
// 默认为UITableViewCellEditingStyleDelete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 返回UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete时进入多选状态
    return UITableViewCellEditingStyleInsert;
}
#pragma mark - editing
// 2.让tableVidw进入编辑状态
// 注意super 和参数
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
#pragma mark - tableView Datasource
// 1>.能否移动, 默认可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 2>.移动完成, 更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    NSLog(@"%@--%ld", _nameArray, _nameArray.count);
    
}
// 3.控制tableView能否进行编辑
// 默认YES可以编辑, NO不能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 5.提交编辑
// 对应按钮的点击方法
// 滑动删除功能只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 添加判断之后会有滑动删除效果
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (35) must be equal to the number of rows contained in that section before the update (35), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'没有更新数据源
#warning 如果对tableView进行删除或添加 先修改数据源 再去进行对应的删除或添加
        // 1.修改数据源
        [_nameArray removeObjectAtIndex:indexPath.row];
        // 2.对cell进行对应的操作
        // 无论删除还是添加, 都已经进行了刷新操作
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [_nameArray insertObject:@"哈斯给" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        NSLog(@"默认删除");
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseIdentifier];
    cell.textLabel.text = [_nameArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
